<template>
  <el-form :model="cloudForm" class="cloudForm">
    <el-form-item>
      <el-col :span="2">
        <span>{{ cloudForm.name }}</span>
      </el-col>
      <el-col :span="1" style="padding-left: 0">
        <span>配送</span>
      </el-col>
      <el-col :span="4">
        <el-select v-model="cloudForm.deliveryExpressId" placeholder="请选择" :disabled="disabled">
          <el-option
            label="顺丰"
            value="0"
          >
            顺丰
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-input v-model="cloudForm.deliveryCost" style="margin-right: 20px;" placeholder="输入费用" :disabled="disabled" @keyup="changeDeliveryCost" />
      </el-col>

      <el-col v-if="isRecovery==0" :span="2" class="el-col-offset-1">
        <span>售后取回</span>
      </el-col>
      <el-col v-if="isRecovery==0" :span="4">
        <el-select v-model="cloudForm.recoveryExpressId" placeholder="请选择" :disabled="disabled">
          <el-option
            label="顺丰"
            value="0"
          >
            顺丰
          </el-option>
        </el-select>
      </el-col>
      <el-col v-if="isRecovery==0" :span="4">
        <el-input v-model="cloudForm.recoveryCost" placeholder="输入费用" :disabled="disabled" @keyup="changeRebackCost" />
      </el-col>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  name: 'CloudStoreExpress',
  props: {
    cloudStore: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      cloudForm: {
        id: null,
        name: null,
        deliveryExpressId: '0',
        deliveryCost: null,
        recoveryExpressId: '0',
        recoveryCost: null
      },
      disabled: false,
      isRecovery: 0
    }
  },
  created() {
    this.cloudForm.id = this.cloudStore.id
    this.cloudForm.name = this.cloudStore.name
  },
  methods: {
    changeDeliveryCost() {
      this.cloudForm.deliveryCost = this.cloudForm.deliveryCost.replace(/[^0-9.]/g, '')
    },
    changeRebackCost() {
      this.cloudForm.recoveryCost = this.cloudForm.recoveryCost.replace(/[^0-9.]/g, '')
    },
    changeDisabled(disabled) {
      this.disabled = disabled
    },
    setData(list) {
      if (list.length > 0) {
        list.forEach(item => {
          if (item.id == this.cloudStore.id) {
            this.cloudStore.deliveryExpressId = item.deliveryExpressId
            this.cloudForm.deliveryCost = item.deliveryCost
            if (item.recoveryCost) {
              this.cloudStore.recoveryExpressId = item.recoveryExpressId
              this.cloudForm.recoveryCost = item.recoveryCost
            }
          }
        })
      }
    },
    setIsRecovery(number) {
      this.isRecovery = number
    },
    getData() {
      if (!this.cloudForm.deliveryExpressId) {
        return this.$message.error('请选择配送物流公司')
      }
      if (!this.cloudForm.deliveryCost) {
        return this.$message.error('请输入配送费用')
      } else if (this.cloudForm.deliveryCost == '1' && !/^((\d|[1-9]\d+)|\.\d{1,2}|(\d|[1-9]\d+)\.\d{1,2})$/g.test(this.cloudForm.deliveryCost)) {
        this.$message.warning('配送费用格式不正确')
      } else if (/^((\d|[1-9]\d+)|\.\d{1,2}|(\d|[1-9]\d+)\.\d{1,2})$/g.test(this.cloudForm.deliveryCost) && this.cloudForm.deliveryCost > 100000) {
        this.$message.warning('配送费用不能大于100000')
      } else if (/^((\d|[1-9]\d+)|\.\d{1,2}|(\d|[1-9]\d+)\.\d{1,2})$/g.test(this.cloudForm.deliveryCost) && this.cloudForm.deliveryCost <= 0) {
        this.$message.warning('配送费用必须大于0')
      }
      if (this.isRecovery == 0) {
        if (!this.cloudForm.recoveryExpressId) {
          return this.$message.error('请选择取回物流公司')
        }
        if (!this.cloudForm.recoveryCost) {
          return this.$message.error('请输入取回费用')
        } else if (this.cloudForm.recoveryCost == '1' && !/^((\d|[1-9]\d+)|\.\d{1,2}|(\d|[1-9]\d+)\.\d{1,2})$/g.test(this.cloudForm.recoveryCost)) {
          this.$message.warning('取回费用不正确')
        } else if (/^((\d|[1-9]\d+)|\.\d{1,2}|(\d|[1-9]\d+)\.\d{1,2})$/g.test(this.cloudForm.recoveryCost) && this.cloudForm.recoveryCost > 100000) {
          this.$message.warning('取回费用不能大于100000')
        } else if (/^((\d|[1-9]\d+)|\.\d{1,2}|(\d|[1-9]\d+)\.\d{1,2})$/g.test(this.cloudForm.recoveryCost) && this.cloudForm.recoveryCost <= 0) {
          this.$message.warning('取回费用必须大于0')
        }
      }

      const data = this.$XEUtils.clone(this.cloudForm, true)
      delete data.name
      if (this.isRecovery == 1) {
        delete data.recoveryExpressId
        delete data.recoveryCost
      }
      return data
    }
  }
}
</script>

<style lang="scss" scoped>
.cloudForm {
  .el-form-item__content {
    margin-left: 0;
  }
}
</style>
